OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//设备备品配件入库
OB.Emes_ActionButton.SparepartsInventoryButton  = function(d, b) {
	var e = b.activeView.viewGrid;
	var a = e.getSelectedRecords();
	var c = a.getLength();
	if (c) {
		// 选择纪录行
		var selectRecords = [];
		for (var i = 0; i < c; i++) {
			selectRecords.push(a[i].id);
			a.hz_id
		}
		OB.Emes_ActionButton.MyRefresh=function(){
			b.refresh();
		}
		OB.RemoteCallManager
				.call(													      
						"com.mes.handler.SparepartInventoryItemQueryHandler",
						{
							ids : selectRecords.join("#####")
						},
						{},
						function(rpcResponse, data, rpcRequest) {
							if (!data.result) {
								alert(data.msg);
								return;
							}
							refershView = b;
							isc.External_SparepartsInventoryButton.create(data).show();
						});
	} else {
		isc.showMessage("请选择下发记录！");
	}
};

isc.defineClass('External_SparepartsInventoryButton',isc.OBPopup);

isc.External_SparepartsInventoryButton.addProperties({

	width : "70%",
	height : "70%",
	title : null,
	showMinimizeButton : false,
	showMaximizeButton : false,

	callPutData : null,
	callPutForm : null,
	okButton : null, 
	cancelButton : null,

	initWidget : function(data) {
		var reqData = data.reqData;
		var LocatorMap = data.LocatorMap;
		
		
		this.callPutForm = isc.ListGrid.create({
			ID : "callPutMaterialForm",
			canEdit : true,
			editByCell : true,
			editEvent : "click",
			data : reqData,
			width : "100%",
			height : 360,
			alternateRecordStyles : true,
			fields : [{
				width : "20%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : true,
				title : '物料编码',
				name : 'ASN_CODE'
			},  {
				width : "25%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : false,
				title : '物料名称',
				name : 'M_NAME'
			}, {
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : false,
				title : '规格型号',
				name : 'M_MODEL'
			}, {
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : true,
				title : '安全库存数量',
				name : 'Remaining'
			}, {
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : true,
				title : '单位',
				name : 'company'
			}, {
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : true,
				title : '厂房区域',
				name : 'processopr'
			},{
				width : "25%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : false,
				title : '储位',
				name : 'M_Locator'
			},{
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : true,
				startRow : true,
				title : '盘点数量',
				name : 'quantity'
			},{
				width : "15%",
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : true,
				startRow : true,
				title : '备注',
				name : 'remarks'
			},{
				type : 'text',
				readOnlyDisplay : "static",
				canEdit : false,
				startRow : false,
				title : '行记录ID',
				name : 'PlanIds'
			}]
		});
		// 隐藏ids
		callPutMaterialForm.hideField('PlanIds');
		// OK Button
		this.okButton = isc.OBFormButton.create({
					title : '确定',
					popup : this,
					action : function() {callPutMaterialForm.saveEdits();
						var flag = true;
						var upResult = callPutMaterialForm.data;
						var cL = upResult.length;
						
						for (var i = 0; i < cL; i++) {
							var Supplier = upResult[i].M_Locator;
							
							
							if(Supplier){
							
							}else{
								isc.showMessage("第"+(i+1)+"行储位不能为空！");
								this.popup.closeClick();
								return ;
							}
						}	
							
						var callback = function(
								rpcResponse,
								data,
								rpcRequest) {
							if (data.message.severity == "success") {
								refershView.refresh();
								isc.showMessage(data.message.text,isc.OBMessageBar.TYPE_INFO);
								rpcRequest.clientContext.popup.closeClick();
							} else {
								isc.showMessage(data.message,isc.OBMessageBar.TYPE_INFO);
							}
							OB.Emes_ActionButton.MyRefresh();

						};
						OB.RemoteCallManager.call("com.mes.handler.SparepartInventorybuttonHandler",{dateParam : upResult}, {}, callback);
						this.popup.closeClick();
					}
				});

		// Cancel Button
		this.cancelButton = isc.OBFormButton.create({
					title : '取消',
					popup : this,
					action : function() {
						this.popup.closeClick();
					}
				});

		//Add the elements into a layout   
		this.items = [ isc.VLayout.create({
			defaultLayoutAlign : "center",
			align : "center",
			width : "100%",
			layoutMargin : 15,
			membersMargin : 70,
			members : [
					isc.HLayout
							.create({
								defaultLayoutAlign : "center",
								align : "center",
								width : "100%",
								height : "80%",
								members : [ this.callPutForm ]
							}),
					isc.HLayout
							.create({
								defaultLayoutAlign : "center",
								align : "center",
								membersMargin : 10,
								marginBottom : 10,
								members : [
										this.okButton,
										this.cancelButton ]
							}) ]
		}) ];

		this.Super('initWidget',arguments);
	}

});
